package org.zjt.spark.sql

import org.apache.spark.SparkConf
import org.apache.spark.sql.SparkSession
import org.apache.spark.sql.functions.col

/**
  * Created by Administrator on 2017/5/10.
  */
object SqlJsonTest extends App {
  val conf = new SparkConf().setAppName("WordCount").setMaster("local")
  val sqlContext = new SparkSession.Builder().config(conf).getOrCreate()
  val peoples = sqlContext.read.json("./person.json")

  peoples.select(col("name"), col("age").plus(1)).show()
  peoples.select(col("name"), col("age")).filter(col("age").gt(21)).show()


  // TODO: 将json关联的数据新建为view/table
  peoples.createOrReplaceTempView("people")
  val names = sqlContext.sql("SELECT name,age FROM people where age = 94 and name = 'li'").rdd.map(_.get(0)).collect()
  println(names.mkString(","))


  // TODO: sqlContext对象的implicits集合  dataSet => 对象
  import sqlContext.implicits._
  val people = sqlContext.read.json("./person.json").as[PersonPojo]
  println(people.rdd.collect().mkString(","))

  peoples.printSchema()


  // TODO: $表示col，将name包装成列
  peoples.select($"name", $"age" + 1).show()
  peoples.filter($"age" > 21).show()


  sqlContext.stop()
}

case class PersonPojo(name: String, age: Long)